﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Windows.Forms;

namespace Utilities
{
    public static class ErpMultiLanguage
    {
        // Fields
        private static CultureInfo _cultureInfo;// = new CultureInfo(strLangCode, true);
        private static XmlDocument doc;
        private static XmlNode sessionPointer;
        private static string strCurrentSession;
        private static string strFileName;
        private static string strLangCode = "vi-VN";

        // Methods

        static ErpMultiLanguage()
        {
            strLangCode = "vi-VN";
            _cultureInfo = new CultureInfo(strLangCode, true);
        }

        private static void CreateSession(string strSessionElement, string strSessionName, XmlNode parentNode)
        {
            XmlNode newChild = null;
            strSessionName = strSessionName.ToUpper();
            newChild = doc.CreateElement(strSessionElement);
            XmlAttribute node = null;
            node = doc.CreateAttribute("name");
            node.Value = strSessionName;
            newChild.Attributes.Append(node);
            parentNode.AppendChild(newChild);
            doc.Save(strFileName);
        }

        private static string GetString(string strTag)
        {
            string str = string.Empty;
            if (doc == null)
            {
                return str;
            }
            XmlNode node = null;
            strTag = strTag.ToUpper();
            if (sessionPointer != null)
            {
                node = sessionPointer.SelectSingleNode(string.Format("Resource[@tag='{0}']", strTag));
                if (node == null)
                {
                    node = doc.SelectSingleNode(string.Format("//Resource[@tag='{0}']", strTag));
                }
            }
            else
            {
                node = doc.SelectSingleNode(string.Format("//Resource[@tag='{0}']", strTag));
            }
            if (node == null)
            {
                return str;
            }
            if (node.Attributes[strLangCode] != null)
            {
                if (node.Attributes[strLangCode].Value != string.Empty)
                {
                    return node.Attributes[strLangCode].Value;
                }
                return node.Attributes["defaultValue"].Value;
            }
            return node.Attributes["defaultValue"].Value;
        }

        public static string GetString(string strSession, string strTag)
        {
            if (doc == null)
            {
                LoadLanguageFile();
            }
            if (strSession != null)
            {
                SetSession(strSession);
            }
            string str = GetString(strTag);
            if (str == string.Empty)
            {
                str = strTag;
            }
            return str;
        }

        public static string GetString(string strSession, string strTag, string strDefaultValue)
        {
            if (doc == null)
            {
                LoadLanguageFile();
            }
            if (strSession != null)
            {
                SetSession(strSession);
            }
            string str = GetString(strTag);
            if (str == string.Empty)
            {
                str = strDefaultValue;
            }
            return str;
        }

        public static string GetString(string strSession, string strTag, string strDefaultValue, bool nested)
        {
            string str = string.Empty;
            if (doc == null)
            {
                LoadLanguageFile();
            }
            if (nested)
            {
                if (strSession == null)
                {
                    str = strDefaultValue;
                }
                else
                {
                    SetSession(strSession);
                    if (sessionPointer == null)
                    {
                        str = strDefaultValue;
                    }
                    else
                    {
                        XmlNode node = null;
                        node = sessionPointer.SelectSingleNode(string.Format("Resource[@tag='{0}']", strTag.ToUpper()));
                        if (node != null)
                        {
                            if (node.Attributes[strLangCode] != null)
                            {
                                if (node.Attributes[strLangCode].Value != string.Empty)
                                {
                                    str = node.Attributes[strLangCode].Value;
                                }
                                else
                                {
                                    str = node.Attributes["defaultValue"].Value;
                                }
                            }
                            else
                            {
                                str = node.Attributes["defaultValue"].Value;
                            }
                        }
                    }
                }
            }
            else
            {
                if (strSession != null)
                {
                    SetSession(strSession);
                }
                str = GetString(strTag);
            }
            if (str == string.Empty)
            {
                str = strDefaultValue;
            }
            return str;
        }

        public static DataTable GetTextFromResource(string strSession)
        {
            DataTable table = new DataTable();
            if (doc == null)
            {
                LoadLanguageFile();
            }
            if (strSession != null)
            {
                SetSession(strSession);
            }
            XmlNodeList childNodes = sessionPointer.ChildNodes;
            DataColumn column = new DataColumn("tag");
            DataColumn column2 = new DataColumn("defaultValue");
            DataColumn column3 = new DataColumn("vi-VN");
            DataColumn column4 = new DataColumn("en-US");
            table.Columns.Add(column);
            table.Columns.Add(column2);
            table.Columns.Add(column3);
            table.Columns.Add(column4);
            for (int i = 0; i < childNodes.Count; i++)
            {
                DataRow row = table.NewRow();
                row["tag"] = childNodes[i].Attributes["tag"].Value;
                row["defaultValue"] = childNodes[i].Attributes["defaultValue"].Value;
                row["vi-VN"] = childNodes[i].Attributes["vi-VN"].Value;
                row["en-US"] = childNodes[i].Attributes["en-US"].Value;
                table.Rows.Add(row);
            }
            return table;
        }

        private static void LoadLanguageFile()
        {
            if ((strFileName == null) || (strFileName == string.Empty))
            {
                strFileName = Application.StartupPath + @"\Languages\Resources.xml";
            }
            if (File.Exists(strFileName))
            {
                if (doc == null)
                {
                    doc = new XmlDocument();
                }
                try
                {
                    doc.Load(strFileName);
                }
                catch
                {
                    doc = null;
                }
            }
        }

        public static void Refresh()
        {
            LoadLanguageFile();
        }

        private static void SetSession(string strSession)
        {
            if (strCurrentSession != strSession)
            {
                sessionPointer = null;
                strCurrentSession = "";
                if (doc != null)
                {
                    sessionPointer = doc.SelectSingleNode(string.Format("//Sessions[@name='{0}']", strSession.ToUpper()));
                    if (sessionPointer == null)
                    {
                        CreateSession("Sessions", strSession, doc.DocumentElement);
                        sessionPointer = doc.SelectSingleNode(string.Format("//Sessions[@name='{0}']", strSession.ToUpper()));
                    }
                    strCurrentSession = strSession;
                }
            }
        }

        public static void SetTextToResource(string strSession, DataTable dtResource)
        {
            if (doc == null)
            {
                LoadLanguageFile();
            }
            if (strSession != string.Empty)
            {
                SetSession(strSession);
            }
            XmlNode newChild = null;
            for (int i = 0; i < dtResource.Rows.Count; i++)
            {
                string str = dtResource.Rows[i]["tag"].ToString().ToUpper();
                newChild = sessionPointer.SelectSingleNode(string.Format("Resource[@tag='{0}']", str));
                if (newChild != null)
                {
                    newChild.Attributes["vi-VN"].InnerText = dtResource.Rows[i]["vi-VN"].ToString();
                    newChild.Attributes["en-US"].InnerText = dtResource.Rows[i]["en-US"].ToString();
                }
                else
                {
                    newChild = doc.CreateElement("Resource");
                    XmlAttribute node = null;
                    node = doc.CreateAttribute("tag");
                    node.Value = str;
                    newChild.Attributes.Append(node);
                    node = doc.CreateAttribute("defaultValue");
                    node.InnerText = dtResource.Rows[i]["defaultValue"].ToString();
                    newChild.Attributes.Append(node);
                    node = doc.CreateAttribute("vi-VN");
                    node.InnerText = dtResource.Rows[i]["vi-VN"].ToString();
                    newChild.Attributes.Append(node);
                    node = doc.CreateAttribute("en-US");
                    node.InnerText = dtResource.Rows[i]["en-US"].ToString();
                    newChild.Attributes.Append(node);
                    sessionPointer.AppendChild(newChild);
                }
            }
            doc.Save(strFileName);
        }

        public static void SetTextToResource(string strSession, string strTag, string strValue)
        {
            if (doc == null)
            {
                LoadLanguageFile();
            }
            if (strSession != null)
            {
                SetSession(strSession);
            }
            XmlNode newChild = null;
            strTag = strTag.ToUpper();
            newChild = sessionPointer.SelectSingleNode(string.Format("Resource[@tag='{0}']", strTag));
            if (newChild != null)
            {
                newChild.Attributes[strLangCode].InnerText = strValue;
            }
            else
            {
                newChild = doc.CreateElement("Resource");
                XmlAttribute node = null;
                node = doc.CreateAttribute("tag");
                node.Value = strTag;
                newChild.Attributes.Append(node);
                node = doc.CreateAttribute("defaultValue");
                node.InnerText = strValue;
                newChild.Attributes.Append(node);
                node = doc.CreateAttribute("vi-VN");
                if (strLangCode == node.Name)
                {
                    node.InnerText = strValue;
                }
                else
                {
                    node.InnerText = "";
                }
                newChild.Attributes.Append(node);
                node = doc.CreateAttribute("en-US");
                if (strLangCode == node.Name)
                {
                    node.InnerText = strValue;
                }
                else
                {
                    node.InnerText = "";
                }
                newChild.Attributes.Append(node);
                sessionPointer.AppendChild(newChild);
            }
            doc.Save(strFileName);
        }

        public static void SetTextToResource(string strSession, string strTag, string strLang, string strValue)
        {
            if (doc == null)
            {
                LoadLanguageFile();
            }
            if (strSession != null)
            {
                SetSession(strSession);
            }
            XmlNode newChild = null;
            strTag = strTag.ToUpper();
            newChild = sessionPointer.SelectSingleNode(string.Format("Resource[@tag='{0}']", strTag));
            if (newChild != null)
            {
                newChild.Attributes[strLang].InnerText = strValue;
            }
            else
            {
                newChild = doc.CreateElement("Resource");
                XmlAttribute node = null;
                node = doc.CreateAttribute("tag");
                node.Value = strTag;
                newChild.Attributes.Append(node);
                node = doc.CreateAttribute("defaultValue");
                node.InnerText = strValue;
                newChild.Attributes.Append(node);
                node = doc.CreateAttribute("vi-VN");
                if (strLang == node.Name)
                {
                    node.InnerText = strValue;
                }
                else
                {
                    node.InnerText = "";
                }
                newChild.Attributes.Append(node);
                node = doc.CreateAttribute("en-US");
                if (strLang == node.Name)
                {
                    node.InnerText = strValue;
                }
                else
                {
                    node.InnerText = "";
                }
                newChild.Attributes.Append(node);
                sessionPointer.AppendChild(newChild);
            }
            doc.Save(strFileName);
        }

        public static void SetTextToResource(string strSession, string strTag, string strDefaultValue, string strVNValue, string strENValue)
        {
            if (doc == null)
            {
                LoadLanguageFile();
            }
            if (strSession != null)
            {
                SetSession(strSession);
            }
            XmlNode newChild = null;
            strTag = strTag.ToUpper();
            newChild = sessionPointer.SelectSingleNode(string.Format("Resource[@tag='{0}']", strTag));
            if (newChild != null)
            {
                newChild.Attributes["vi-VN"].InnerText = strVNValue;
                newChild.Attributes["en-US"].InnerText = strENValue;
            }
            else
            {
                newChild = doc.CreateElement("Resource");
                XmlAttribute node = null;
                node = doc.CreateAttribute("tag");
                node.Value = strTag;
                newChild.Attributes.Append(node);
                node = doc.CreateAttribute("defaultValue");
                node.InnerText = strDefaultValue;
                newChild.Attributes.Append(node);
                node = doc.CreateAttribute("vi-VN");
                node.InnerText = strVNValue;
                newChild.Attributes.Append(node);
                node = doc.CreateAttribute("en-US");
                node.InnerText = strENValue;
                newChild.Attributes.Append(node);
                sessionPointer.AppendChild(newChild);
            }
            doc.Save(strFileName);
        }

        // Properties
        public static CultureInfo CurrentCultureInfo
        {
            get
            {
                return _cultureInfo;
            }
            set
            {
                _cultureInfo = value;
                strLangCode = _cultureInfo.Name;
                doc = null;
                strCurrentSession = null;
            }
        }

        public static string CurrentLanguage
        {
            get
            {
                return strLangCode;
            }
            set
            {
                strLangCode = value;
                doc = null;
                strCurrentSession = null;
            }
        }

        public static string LanguageFileName
        {
            get
            {
                return strFileName;
            }
            set
            {
                strFileName = value;
            }
        }
    }
}
